#include "UI\Controls\Control.h"
#include "Base\Surface.h"

///////////////////////////////////////////////////////////
//        ///  //////  /        ///  ///////  ///        //
//  //////  /  //////  /  //////  /  ///////  /  //////////
//        ///  //////  /        ///  ///////  /  //////////
//  /////////  //////  /  //////  /  ///////  /  //////////
//  ///////////      ///        ///        /  ///        //
///////////////////////////////////////////////////////////

#pragma message("TODO: Load Font from settings.")
bool ShRp::Base::Surface::Init()
{
	m_Graphics = NULL;
	m_PaintStruct = new PAINTSTRUCT();
	return true;
}

void ShRp::Base::Surface::Terminate()
{

}

void ShRp::Base::Surface::Line( int2 p1, int2 p2, Color _Color )
{
	assert(m_Graphics != NULL);
	const Gdiplus::Pen pen(Surface::ToGDIColor(_Color), 1.0f);
	m_Graphics->DrawLine(&pen, p1.x, p1.y, p2.x, p2.y);
}

void ShRp::Base::Surface::Box( int2 p1, int2 p2, Color _Color )
{
	assert(m_Graphics != NULL);
	const Gdiplus::Pen pen(Surface::ToGDIColor(_Color), 1.0f);
	m_Graphics->DrawRectangle(&pen, p1.x, p1.y, (p2.x - p1.x), (p2.y - p1.y));
}

void ShRp::Base::Surface::Bar( int2 p1, int2 p2, Color _Color )
{
	assert(m_Graphics != NULL);
	int2 size = p2 - p1;
	Gdiplus::SolidBrush brush(Surface::ToGDIColor(_Color));
	m_Graphics->FillRectangle(&brush, p1.x, p1.y, size.x, size.y);
}

void ShRp::Base::Surface::Circle( int2 p, int r, Color _Color )
{
	assert(m_Graphics != NULL);
	const Gdiplus::Pen pen(Surface::ToGDIColor(_Color), 1.0f);
	m_Graphics->DrawEllipse(&pen, p.x - r, p.y - r, p.x + r, p.y + r);
}

void ShRp::Base::Surface::Print( const char* _Text, int2 _Pos, Color _Color )
{
	assert(m_Graphics != NULL);
	Gdiplus::SolidBrush brush(Surface::ToGDIColor(_Color));
	m_Graphics->DrawString(Surface::ToWString(_Text).c_str(), strlen(_Text), m_Font, Gdiplus::PointF(Gdiplus::REAL(_Pos.x), Gdiplus::REAL(_Pos.y)), &brush);
}

void ShRp::Base::Surface::SetClearColor( Color _Color )
{
	m_ClearColor = _Color;
}

void ShRp::Base::Surface::SetFont( const char* _FontName, int _Size )
{
	if(m_Font!= NULL)
	{
		delete m_Font;
	}
	m_Font = new Gdiplus::Font(Surface::ToWString(_FontName).c_str(), Gdiplus::REAL(_Size));
}

void ShRp::Base::Surface::StartPaint( UI::Control* _Control )
{
	HDC hdc = ::BeginPaint(_Control->GetHWND(), m_PaintStruct);
	m_Graphics = new Gdiplus::Graphics(hdc);
	m_Graphics->Clear(Surface::ToGDIColor(m_ClearColor));
}

void ShRp::Base::Surface::EndPaint( UI::Control* _Control )
{
	::EndPaint(_Control->GetHWND(), m_PaintStruct);
	delete m_Graphics;
	m_Graphics = NULL;
}

////////////////////////////////////////////////////////////////
//      ///      ///  /  //////  ///    ///          /        //
//  ////  /  ////  /  /  //////  /  ////  /////  /////  ////////
//      ///      ///  ///  //  ///        /////  /////      ////
//  ///////  //  ///  ///  //  ///  ////  /////  /////  ////////
//  ///////  ////  /  /////  /////  ////  /////  /////        //
////////////////////////////////////////////////////////////////

PAINTSTRUCT* ShRp::Base::Surface::m_PaintStruct;
Gdiplus::Graphics* ShRp::Base::Surface::m_Graphics;
Gdiplus::Font* ShRp::Base::Surface::m_Font;
Color ShRp::Base::Surface::m_ClearColor;


Gdiplus::Color ShRp::Base::Surface::ToGDIColor( Color _Color )
{
	return Gdiplus::Color(_Color.w, _Color.r, _Color.g, _Color.b);
}

std::wstring ShRp::Base::Surface::ToWString( const char* _Str )
{
	const int len = strlen(_Str) + 1;
	wchar_t* buffer = new wchar_t[len];
	MultiByteToWideChar(CP_ACP, 0, _Str, len, buffer, len);
	std::wstring wstr(buffer);
	delete[] buffer;
	return wstr;
}